/**
 * 作者: wgx
 * 日期: 2025-02-21 16:09:22
 * 备注: 用户怪兽洞窟模块数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserCaveInfo;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserCaveInfoModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 用户ID
    protected long secondRewardTime; // 领取每秒奖励的时间戳
    protected int onceRewardId; // 首达奖励的领取进度

    public void reset() {
    }

    protected String getKey() {
        return "UserCaveInfo." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_cave_info"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 用户ID
        obj.put("secondRewardTime", secondRewardTime); // 领取每秒奖励的时间戳
        obj.put("onceRewardId", onceRewardId); // 首达奖励的领取进度
        return obj;
    }

    public PBUserCaveInfo.Builder takeInitBuilder() {
        PBUserCaveInfo.Builder sb = PBUserCaveInfo.newBuilder();
        if (secondRewardTime != 0) {
            sb.setSecondRewardTime(secondRewardTime);
        }
        if (onceRewardId != 0) {
            sb.setOnceRewardId(onceRewardId);
        }
        return sb;
    }

    public PBUserCaveInfo.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserCaveInfo.Builder sb = PBUserCaveInfo.newBuilder();
        if (isFieldUpdate("secondRewardTime")) {
            sb.setSecondRewardTime(secondRewardTime);
        }
        if (isFieldUpdate("onceRewardId")) {
            sb.setOnceRewardId(onceRewardId);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getSecondRewardTime() {
        return secondRewardTime;
    }

    public void putSecondRewardTime(long secondRewardTime) {
        if (this.secondRewardTime != secondRewardTime) {
            this.secondRewardTime = secondRewardTime;
            updateRedisHashField(getKey(), "secondRewardTime", this.secondRewardTime);
        }
    }

    public void addSecondRewardTime(long value) {
        this.secondRewardTime += value;
        updateRedisHashField(getKey(), "secondRewardTime", this.secondRewardTime);
    }

    public int getOnceRewardId() {
        return onceRewardId;
    }

    public void putOnceRewardId(int onceRewardId) {
        if (this.onceRewardId != onceRewardId) {
            this.onceRewardId = onceRewardId;
            updateRedisHashField(getKey(), "onceRewardId", this.onceRewardId);
        }
    }

    public void addOnceRewardId(int value) {
        this.onceRewardId += value;
        updateRedisHashField(getKey(), "onceRewardId", this.onceRewardId);
    }

}